<?php

class MCustomer extends Model {

    function __construct() {
        parent::__construct();
        $this->table = "cms_customer";
    }

    //搜索客户列表
    public function selectLists($search,$page,$page_size=20) {
        $sql_data = array('is_delete' => '0', 'company_id' => '1');
        $search_field = array('name','mobile','address');
        $result = $this->selectAll($this->table,$sql_data,$page,$page_size,$search,$search_field);
        return $result;
    }
    
    //搜索客户总数
    public function selectCounts($search) {
        $sql_data = array('is_delete' => '0', 'company_id' => '1');
        $search_field = array('name','mobile','address');
        $result = $this->selectCount($this->table,$sql_data, $search,$search_field);
        return $result;
    }

    //客户详情
    public function selectInfo($id) {
        $sql_data = array('id' => $id);
        $result = $this->selectRow($this->table, $sql_data);
        return $result;
    }

    //新增或修改客户
    public function inUp($data) {
        $id = $data['id'];
        unset($data['id']);
        if(!empty($id)){
            $result = $this->updateRow($this->table, $data, " where id=".$id);
        }  else {
            $result = $this->insertRow($this->table, $data);
        }
        return $result;
    }
    
    //修改客户余额
    public function updateDiscount($cus_id,$money,$symbol='-'){
        if(strcmp($symbol, '=') == 0){
            $balance = " balance=".$money;
        }  else {
            $balance = " balance=balance".$symbol.$money;
        }
        $sql = " update $this->table set $balance";
        $sql .= " where id=$cus_id";
        $result = $this->_db->exec($sql);
        return $result;
    }

    //查找客户
    public function search($search, $select = "*", $is_count = 0, $page = 1, $page_size = 5) {
        $con = "is_delete=0";
        $limit = '';
        if(!empty($search)) {
            $con .= " and (id=:id_search or (name like :search or mobile like :search or address like :search))";
            $new_sql_data = array(':id_search' => $search, ':search' => '%' . $search . '%');
        }
        if ($is_count != 1) {
            $limit = " limit ".($page-1)*$page_size.",".$page_size;
        }
        $sql = "select $select from $this->table where $con $limit";
        if ($is_count == 1) {
            $result = $this->_db->fetchRow($sql, $new_sql_data);
        } else {
            $result = $this->_db->fetchAll($sql, $new_sql_data);
        }
        return $result;
    }
     //删除订单
    public function del($id) {
        $data = array('is_delete' => 1);
        $where = " where id= $id";
        $result = $this->updateRow($this->table, $data, $where);
        return $result;
    }

}
